package com.scaffold.chapter.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.scaffold.BasicBizAction;
import com.scaffold.chapter.entity.Chapter;
import com.scaffold.chapter.service.ChapterService;

@Controller
public class ChapterJsonAction extends BasicBizAction{
	@Autowired
	private ChapterService chapterService;
	
	@RequestMapping("/chapter/save.json")
	public String save(Long id){
		if(id==null){
			Chapter chapter = new Chapter();
			this.bindParams(chapter);
			this.setOwner(chapter);
			this.chapterService.save(chapter);
		}else{
			Chapter chapter = this.chapterService.getChapter(id);
			this.checkOwner(chapter);
			this.bindParams(chapter);
			this.chapterService.update(chapter);
		}
		return this.getSuccessResult();
	}

	@RequestMapping("/chapter/get.json")
	public String get(Long id){
		Chapter chapter = chapterService.getChapter(id);
		this.checkOwner(chapter);
		return this.getResult("0", null, chapter);
	}

	@RequestMapping("/chapter/delete.json")
	public String delete(Long id){
		Chapter chapter = chapterService.getChapter(id);
		this.checkOwner(chapter);
		this.chapterService.delete(chapter);
		return this.getSuccessResult();
	}

}
